package com.zcsy.manage.web.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.AppConfig;
import com.zcsy.manage.model.mall.GoodsCategory;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.IAppConfigService;
import com.zcsy.manage.service.base.IShopCategoryService;
import com.zcsy.manage.service.mall.IGoodsCategoryService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;

/**
 * @Title: AppConfigController.java
 * @Package： com.zcsy.manage.web.base 
 * @Description: 客户端小图片配置service层接口实现类
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年6月30日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */

@Controller
@RequestMapping(value = "/appConfig")
public class AppConfigController {
	
	@Autowired 
	private IAppConfigService appConfigService;
	
	@Autowired 
	private IGoodsCategoryService goodsCategoryService;

	@Autowired
	private IDictService dictService;
	
	@Autowired
	private IShopCategoryService shopCateService;
	
    /**
     * @Description 查询图标列表<br>
     * 
     * @param model 
     * @param pNo 当前页数
     * @param imgName 图标名称
     * @param objType 图标类型
     * @param req 用户请求
     * @return 返回图标列表
     * @author caiyinli 13277030520@163.com
     * @date 2016年6月30日
     * @see 
     */
    @RequestMapping(value="appConfigList" , method = RequestMethod.GET)
    public String appConfigList(Model model, Integer pNo, Integer pSize, String imgName, Integer objType,HttpServletRequest req){
    	
	    if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
		
		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
       
        
        AppConfig appConfig = new AppConfig();
        
        if(StringUtils.isNotEmpty(imgName)){
        	appConfig.setImgName(imgName);
        }
        if(objType == null){
        	objType = -1;
        }
   
        if(objType != -1){
        	appConfig.setObjType(objType);
        } else {
            appConfig.setObjType(Constants.MENU_SELECT_NONE);
        }
        
        //此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, pageSize);
        List<AppConfig> appConfigs = appConfigService.getAppConfigByKeyword(appConfig);
        PageInfo<AppConfig> info = new PageInfo<AppConfig>(appConfigs);
         
        List<GoodsCategory> goodsCategories = goodsCategoryService.getAllGoodsCategory();
        
		model.addAttribute("appConfig", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("imgName", imgName);
		model.addAttribute("objType", objType);
		model.addAttribute("goodsCategories", goodsCategories);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

        return "base/appconfig/appConfigList";
    }
    
    /**
     * @Description 返回新增或编辑图标页面<br>
     * type = 1:新增页面
     * type = 2:编辑页面
     * 
     * @param model
     * @param id 主键id id=0新增 ；id!=0编辑
     * @return 返回新增或编辑图标页面
     * @author caiyinli 13277030520@163.com
     * @date 2016年3月30日
     * @see 
     */
    @RequestMapping(value = "editAppConfig", method = RequestMethod.GET)
    public String editAppConfig(Model model, String id, HttpServletRequest request) {
        try {
            if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
                model.addAttribute("type", Constants.SAVE_TYPE);
            } else {
                AppConfig appConfig = appConfigService.getAppConfigByPrimaryKey(id);
                if(appConfig != null){
                    model.addAttribute("type", Constants.EDIT_TYPE);
                	model.addAttribute("appConfig", appConfig);
                }
            }
            
            model.addAttribute("shopCates", shopCateService.getAllShopcategory());
        } catch (Exception e) {
        }
        return "base/appconfig/appConfigEdit";
    }
    
    /**
     * @Description 编辑或新增图标信息<br>
     * 
     * @param id 图标主键id
     * @param imgName 图标名称
     * @param objType 图标类型
     * @param imgUrl 图标地址
     * @param request 用户请求
     * @param response 服务器响应
     * @return 1：成功 0：失败
     * @author caiyinli 13277030520@163.com
     * @date 2016年6月30日
     * @see 
     */
    @RequestMapping(value = "saveOrUpdateAppConfig", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveOrUpdateGoodsInfo(String id,String imgName, Integer objType,String imgUrl,Integer sort, String cateType , HttpServletRequest request,HttpServletResponse response) {
        RequestJson result = new RequestJson();
        User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        try {
        	AppConfig record = new AppConfig();
            record.setImgName(imgName);
            record.setObjType(objType);
            record.setImgUrl(imgUrl);
            record.setSort(sort);
            record.setShopCategoryId(cateType);
            
            if(id != ""&& !"0".equals(id)){
            	record.setId(id);
            	appConfigService.update(record);
            	LogUtils.insertLog("用户更新APP图标", user.getId(), request);
            }else{
            	record.setId(UniqueNumberUtils.get18UniqueNumber());
            	record.setSendTime(DateUtils.getFmtDate());
            	record.setUserAccount(user.getLoginname());
            	appConfigService.insert(record);
            	LogUtils.insertLog("用户新增APP图标", user.getId(), request);
            }
        }catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作异常");
        }
        
        return result;
    }
}
